package com.xique.blelock.view.AcountManager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.hss01248.dialog.StyledDialog;
import com.hss01248.dialog.interfaces.MyItemDialogListener;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.xique.blelock.NetUtils.XqNetClient;
import com.xique.blelock.R;
import com.xique.blelock.Utils.ACache;
import com.xique.blelock.Utils.Stringkey;
import com.xique.blelock.Utils.TimeUtil;
import com.xique.blelock.model.UserInfo;
import com.xique.blelock.view.BaseActivity;

import org.json.JSONException;
import org.json.JSONObject;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Ediot_Activity extends BaseActivity implements View.OnClickListener {

    private String TAG = "Ediot_Activity";
    private String uidflag = "1";
    private String tokenflag1 = "1";
    private String namesre="小荷";

    @BindView(R.id.edituser)
    TextView edituser;


    @BindView(R.id.rightbutton)
    TextView save;

    @BindView(R.id.editsex)
    TextView editsex;

    @BindView(R.id.title)
    TextView title;

    private UserInfo userInfo;

    BottomSheetDialog bottomSheetDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_activity);
        ButterKnife.bind(this);
        initview();

    }

    private void initview() {

        save.setVisibility(View.VISIBLE);
        save.setText("完成");
        save.setTextColor(getResources().getColor(R.color.primary));

        title.setText("个人信息");


        userInfo = (UserInfo) ACache.get(this).getAsObject(Stringkey.USERInfo);


        if(!TextUtils.isEmpty(userInfo.getNickname()))
        {
            edituser.setText(userInfo.getNickname());
        }
        if(userInfo.getSex() == 1)
        {
            editsex.setText("男生");
        }else{
            editsex.setText("女生");
        }

        bottomSheetDialog = new BottomSheetDialog(this);
        bottomSheetDialog.setContentView(R.layout.sexlayout);

        bottomSheetDialog.findViewById(R.id.nan).setOnClickListener(this);
        bottomSheetDialog.findViewById(R.id.nv).setOnClickListener(this);
        bottomSheetDialog.findViewById(R.id.cancle).setOnClickListener(this);
    }

    @OnClick({R.id.sexlayout, R.id.namelayout, R.id.BACKER})
    public void modifyUserinfo(View view){
        if(view.getId() == R.id.sexlayout){
            showSimpleBottomSheetList();

        }else if(view.getId() == R.id.namelayout){
            showEditTextDialog();
        }else if(view.getId() == R.id.BACKER){
            finish();
        }

    }

    // ================================ 生成不同类型的BottomSheet
    private void showSimpleBottomSheetList() {
        bottomSheetDialog.show();
    }




    private void showEditTextDialog() {

        new MaterialDialog.Builder(this)
                .title("修改名字")// 标题
                .inputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME)// 输入类型

                .input("在此输入您的昵称", "", new MaterialDialog.InputCallback() {
                    @Override
                    public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
                        //Toast.makeText(FingerListActivty.this, "do something...", Toast.LENGTH_SHORT).show();

                    }
                })
                .negativeText("取消")
                .negativeColor(getResources().getColor(R.color.grey))
                .positiveText("确定")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        if(TextUtils.isEmpty(dialog.getInputEditText().getText())){
                            showToast(Ediot_Activity.this,"请输入名字");
                            return;
                        }
                        edituser.setText(dialog.getInputEditText().getText());
                    }
                })
                .show();// 显示对话框
    }

    @OnClick(R.id.BACKER)
    public void BackLastPage(){
        finish();
    }


    @OnClick(R.id.rightbutton)
    public void saveName()
    {
        setUserInfo();
    }


    /**
     * 设置用户信息
     */
    private void setUserInfo() {
        XqNetClient netClient = new XqNetClient();
        final JSONObject jsonobject = new JSONObject();
        if(TextUtils.isEmpty(edituser.getText())){
            showToast(this, "请输入姓名");
            return;
        }
        final String usersex ;
        if(TextUtils.isEmpty(editsex.getText()))
        {
            usersex = "女生";
        } else{
            usersex = editsex.getText().toString();
        }

        String newname = edituser.getText().toString();


            try {
                jsonobject.put("token", userInfo.getToken());
                jsonobject.put("uid", userInfo.getUid());
                jsonobject.put("name", newname);
                jsonobject.put("age", 25);
                jsonobject.put(Stringkey.Sexkey, usersex.equals("男生")?1:0);
                jsonobject.put("timestamp", TimeUtil.getSystemCurrentTimeLong()+"");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            showProgressDialog();
            netClient.updateUserInfo(jsonobject.toString()).enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    String code = "1";
                    String names = "";
                    String sexs = "";
                    try {
                        JSONObject jsonObject1 = new JSONObject(response.body());
                        code = jsonObject1.getString("message");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (code.equals("成功")) {//如果成功就
                    userInfo.setNickname(edituser.getText().toString());
                        try {
                            userInfo.setSex(jsonobject.getInt(Stringkey.Sexkey));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        userInfo.setSetting(true);
                    ACache.get(Ediot_Activity.this).put(Stringkey.USERInfo, userInfo);
                    showToast(Ediot_Activity.this, "信息更新完成");
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                    }
                    hideProgressDialog();
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    t.printStackTrace();

                    hideProgressDialog();
                }
            });
    }

    public Ediot_Activity() {
        super();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "收到数据 uid:"+uidflag+"收到数据token:"+tokenflag1);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
    }

    private void dialog() {


    }

    public void alert_edit(){
        final EditText et = new EditText(this);
        new AlertDialog.Builder(this).setTitle("请输入昵称") .setIcon(android.R.drawable.sym_def_app_icon)
                .setView(et)
                .setPositiveButton("确定",
                   new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //按下确定键后的事件
                namesre = et.getText().toString();
            }
        }).setNegativeButton("取消",null).show();
    }



    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.nan){
            bottomSheetDialog.dismiss();
            editsex.setText("男生");
        }else if(v.getId() == R.id.nv){
            bottomSheetDialog.dismiss();
            editsex.setText("女生");
        }else if(v.getId() == R.id.cancle){
            bottomSheetDialog.dismiss();
        }
    }
}
